package com.boco.blch.analysiser.compliance.HW.data;

import com.boco.blch.analysiser.compliance.ComplianceResolved;
import com.boco.blch.analysiser.util.XmlUtil;

/**
 * HDFS客户端和服务端传输加密安全基线要求项【控制节点】+【数据节点】
 * 【控制节点】
 * 1、配置文件core-site.xml文件的配置项hadoop.rpc.protection的值为 privacy
 * 2、配置文件hdf-site.xml文件的配置项dfs.http.policy的值为HTTPS_ONLY
 * 
 * 
 * 【数据节点】
 * 1、配置文件core-site.xml文件的配置项hadoop.rpc.protection的值为 privacy
 * 2、配置文件hdf-site.xml文件的配置项dfs.http.policy的值为HTTPS_ONLY
 * 
 * @author admin
 *
 */
public class BOCO_Hdfs_DataEncryptProtect_ClientEncrypt implements ComplianceResolved {
	
	private final static String core_name = "hadoop.rpc.protection";
	private final static String core_value = "privacy";
	private final static String hdfs_name = "dfs.http.policy";
	private final static String hdfs_value = "HTTPS_ONLY";

	@Override
	public int realEchoResolved(String echoResult) {
		int result = FAILED;
		if(echoResult != null && echoResult.length() > 0){
			if(echoResult.indexOf("NameNode-start") != -1){//控制节点
				String strResult = echoResult.substring(echoResult.indexOf("NameNode-start")+"NameNode-start".length(), echoResult.indexOf("NameNode-end")).trim();
				result = getResult(strResult);
			}
			if(result == FAILED && echoResult.indexOf("JournalNode-start") != -1){//控制节点
				String strResult = echoResult.substring(echoResult.indexOf("JournalNode-start")+"JournalNode-start".length(), echoResult.indexOf("JournalNode-end")).trim();
				result = getResult(strResult);
			}
			if(result == FAILED && echoResult.indexOf("DataNode-start") != -1){//数据节点
				String strResult = echoResult.substring(echoResult.indexOf("DataNode-start")+"DataNode-start".length(), echoResult.indexOf("DataNode-end")).trim();
				result = getResult(strResult);
			}
		}
		return result;
	}
	
	/**
	 * 1、配置文件core-site.xml文件的配置项hadoop.rpc.protection的值为 privacy
	 * 2、配置文件hdf-site.xml文件的配置项dfs.http.policy的值为HTTPS_ONLY
	 * @param echoResult
	 * @return
	 */
	private int getResult(String echoResult){
		int result = FAILED;
		if(echoResult != null && echoResult.indexOf("core-site-start") != -1){//yarn-site.xml解析
			String str1 = echoResult.substring(echoResult.indexOf("core-site-start")+"core-site-start".length(), echoResult.indexOf("core-site-end"));
			String value = "";
			if(str1 != null && str1.length() > 0){//.contains(NotFindFile)){//目录不存在
//				result = SUCCESS;
//			}else{
				value = XmlUtil.getValueByName(str1, core_name).trim();
				if(value != null && value.equals(core_value)){
					result = SUCCESS;
				}else{
					result = FAILED;
				}
			}
			if(result == SUCCESS){
				//capacity-scheduler.xml 的配置项yarn.scheduler.capacity.root.queues的值，
				String str2 = echoResult.substring(echoResult.indexOf("hdfs-site-start")+"hdfs-site-start".length(), echoResult.indexOf("hdfs-site-end"));
				if(str2 != null && str2.length() > 0){//.contains(NotFindFile)){//目录不存在
//					result = SUCCESS;
//				}else{
					value = XmlUtil.getValueByName(str2, hdfs_name).trim();
					if(value != null && value.equals(hdfs_value)){
						result = SUCCESS;
					}else{
						result = FAILED;
					}
				}else{
					result = FAILED;
				}
			}
		}
		return result;
	}
}
